import java.util.Scanner;

import client.ClientMessageHandler;
import client.ServerAccess;
import when3meet.view.Application;
import xml.Message;

/** Launch command-line Client to show minimal access needs. */
public class ClientLauncher {

	/**
	 * Note that to simplify the coding of this command-client, it declares that
	 * it can throw an Exception, which is typically the failed connection to a
	 * server.
	 */
	public static void main(String[] args) throws Exception {
		// FIRST thing to do is register the protocol being used. There will be
		// a single class protocol
		// that will be defined and which everyone will use. For now,
		// demonstrate with skeleton protocol.
		if (!Message.configure("http://www.draw2choose.com/draw2choose.xsd")) {
			System.exit(0);
		}

		ServerAccess sa = new ServerAccess("72.249.186.243");
		System.out.println(sa.getHost());
		sa.connect(new ClientMessageHandler());

		Application app = Application.getInstance();
		app.setServerAccess(sa);

		// This controller is an anonymous class who responds to closing events.
		// by exiting the application.

		// send an introductory connect request
		String xmlString = Message.requestHeader() + "<connectRequest/></request>";
		Message m = new Message(xmlString);

		sa.sendRequest(m);

		// await response. If we don't stop ServerAccess manually, there will be
		// a background thread
		// the continually runs and the program will never terminate.
		// This problem is not as noticeable in GUI-based clients.
		System.err.println("Press Return to terminate client.");
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		sc.nextLine();

		System.out.println("Client disconnected.");
		sa.disconnect();
	}
}
